package com.czhhx.myedcon.ui;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.aries.ui.view.title.TitleBarView;
import com.czhhx.myedcon.R;
import com.czhhx.myedcon.entity.UserEntity;

/**
 * @Description com.czhhx.myedcon.ui
 * @Auth czh
 * @Date 2023/3/30
 **/
public class RegisterActivity extends Activity {
    /**
     * 声明控件
     */
    TitleBarView titleBar;
    TextView tvRegister;
    EditText editPhone, editPassword;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        /**
         * 初始化布局
         */
        setContentView(R.layout.activity_register);
        /**
         * 初始化控件
         */
        initView();
    }

    private void initView() {
        tvRegister = findViewById(R.id.tvRegister);
        editPhone = findViewById(R.id.editPhone);
        editPassword = findViewById(R.id.editPassword);
        titleBar = findViewById(R.id.titleBar);
        titleBar.setStatusBarLightMode(true);
        titleBar.setTitleMainText("注册");
        titleBar.setLeftTextDrawable(R.mipmap.ic_xmfh_back);
        titleBar.setTextColor(Color.parseColor("#000000"));
//        点击返回按钮
        titleBar.setOnLeftTextClickListener(view -> {
            finish();
        });
        /**
         * 点击注册按钮
         */
        tvRegister.setOnClickListener(view -> {
            /**
             * 判断手机号和密码是否为空
             */
            if (getEdit(editPhone)) {
                showToast("手机号不能为空");
                return;
            }
            if (getEdit(editPassword)) {
                showToast("密码不能为空");
                return;
            }
            /**
             * 保存手机号及密码
             */
            UserEntity userEntity = new UserEntity();
            userEntity.setPhone(editPhone.getText().toString());
            userEntity.setPassword(editPassword.getText().toString());
            userEntity.save();
            /**
             * 跳转登录界面
             */
            Intent intent = new Intent(this, LoginActivity.class);
            startActivity(intent);
            finish();
        });

    }

    /**
     * 提示弹框方法
     *
     * @param s
     */
    private void showToast(String s) {
        Toast.makeText(this, s, Toast.LENGTH_LONG).show();
    }

    /**
     * 根据输入框返回内容方法
     *
     * @param edit
     * @return
     */
    private boolean getEdit(EditText edit) {
        return edit.getText().toString().isEmpty();
    }
}
